package com.kxq.zhuoyue.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kxq.zhuoyue.pageUtilEntity.Page;
import com.kxq.zhuoyue.po.AskAnswer;
import com.kxq.zhuoyue.service.AskAnswerService;
import com.kxq.zhuoyue.util.UUID;
import com.kxq.zhuoyue.util.result.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xjh
 * @since 2023-09-01
 */
@RestController
@RequestMapping("/askAnswer")
public class AskAnswerController {
    @Autowired
    private AskAnswerService askAnswerService;

    @PostMapping("/getById")
    public AjaxResult getById(@RequestBody Page page) {
        Map map = new HashMap<>();
        LambdaQueryWrapper<AskAnswer> queryWrapper = new LambdaQueryWrapper<>();
//    queryWrapper.like(Material::getMaterialTitle,page.getMaterialTitle());
        queryWrapper.eq(AskAnswer::getGroupId, page.getAskAnswerGroupId());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<AskAnswer> list = askAnswerService.list(queryWrapper);
        PageInfo pageInfo = new PageInfo<>(list);
        map.put("data", list);
        map.put("pageSize", pageInfo.getPageSize());
        map.put("total", pageInfo.getTotal());
        return AjaxResult.success("获取成功 !", map);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody AskAnswer askAnswer) {
        String id = UUID.getOrderIdByTime();
        askAnswer.setAskAnswerId(id);
        askAnswer.setAskAnswerStatus("待审核");
        boolean save = askAnswerService.save(askAnswer);
        if(save){
            return AjaxResult.success("添加成功 ！");
        }else {
            return AjaxResult.success("添加失败 ！");
        }
    }


    @PostMapping("/update")
    public AjaxResult update(@RequestBody AskAnswer askAnswer){
        boolean update = askAnswerService.updateById(askAnswer);
        if(update){
            return AjaxResult.success("修改成功 ！");
        }else {
            return AjaxResult.success("修改失败 ！");
        }
    }

    @PostMapping("/delete/{askAnswerId}")
    public AjaxResult delete(@PathVariable String askAnswerId){
        boolean b = askAnswerService.removeById(askAnswerId);
        if(b){
            return AjaxResult.success("删除成功 ！");
        }else {
            return AjaxResult.success("删除失败 ！");
        }
    }

    @PostMapping("/getByExample")
    public AjaxResult getByExample(@RequestBody Page page){
        Map map = new HashMap<>();
        LambdaQueryWrapper<AskAnswer> queryWrapper = new LambdaQueryWrapper<>();
//    queryWrapper.like(Material::getMaterialTitle,page.getMaterialTitle());
//        queryWrapper.eq(AskAnswer::getGroupId, page.getAskAnswerGroupId());
        queryWrapper.like(AskAnswer::getAskAnswerAsk,page.getAskAnswerAsk());
        queryWrapper.like(AskAnswer::getKeyWords,page.getKeyWords());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<AskAnswer> list = askAnswerService.list(queryWrapper);
        PageInfo pageInfo = new PageInfo<>(list);
        map.put("data", list);
        map.put("pageSize", pageInfo.getPageSize());
        map.put("total", pageInfo.getTotal());
        return AjaxResult.success("获取成功 !", map);
    }
}

